﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml;

namespace Songhay.Tests
{
    /// <summary>
    /// General research tests.
    /// </summary>
    [TestClass]
    public class FrameworkFileTest
    {
        [TestInitialize]
        public void ClearPreviousTestResults()
        {
            var directory = Directory.GetParent(TestContext.TestDir);

            directory.GetFiles()
                .OrderByDescending(f => f.LastAccessTime).Skip(1)
                .ToList().ForEach(f => f.Delete());

            directory.GetDirectories()
                .OrderByDescending(d => d.LastAccessTime).Skip(1)
                .ToList().ForEach(d => d.Delete(true));
        }

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        [TestMethod]
        public void ShouldGetPathLength()
        {
            var directory = new DirectoryInfo(@"E:\~shares\dataRoot\Visual Studio 2010\Projects (Public)\");
            var dict = new Dictionary<string, int>();
            AddPathsToList(directory, dict);
            dict.ToList()
                .ForEach(d =>
                {
                    if(d.Value > 200)
                        TestContext
                            .WriteLine(string.Format("{0}:{1}", d.Key, d.Value.ToString()));
                });
        }

        void AddPathsToList(DirectoryInfo info, Dictionary<string, int> dict)
        {
            info.GetDirectories()
                .ToList()
                .ForEach(d =>
                {
                    dict.Add(d.FullName, d.FullName.Length);
                    d.GetDirectories()
                        .ToList()
                        .ForEach(d2 =>
                        {
                            AddPathsToList(d2, dict);
                        });
                });
        }

        [TestMethod]
        public void ShouldGetRelativePath()
        {
            var pathToFolder = @"E:\~shares\dataRoot\Visual Studio 2010\Projects\Songhay.Web.Mvc.ServerIndex\Content\Samples\css\";
            Assert.IsTrue(Directory.Exists(pathToFolder));
        }

        [TestMethod]
        public void ShouldGetTypeName()
        {
            var name = this.GetType().FullName;
            TestContext.WriteLine(name);
        }

        [TestMethod]
        public void ShouldWriteTextFileWithStreamWriter()
        {
            var fileText = @"
This is the text to write to the test file.
This sentence should be on a line all by iteself.
According to Notepad++ this file should be in ANSI format by default.
According to Notepad++, when Encoding.UTF8 is specified the encoding is UTF8.
This is the end of the file.
            ";
            var path = @"E:\~shares\dataRoot\Visual Studio 2010\Projects\Songhay.Tests\FrameworkFileTest.txt";

            var stream = new FileStream(path, FileMode.Create);
            try
            {
                using(var sw = new StreamWriter(stream, Encoding.UTF8))
                {
                    sw.Write(fileText);
                }
            }
            finally
            {
                if(stream != null) stream.Dispose();
            }
        }

        [TestMethod]
        public void ShouldWriteTextFileWithXmlTextWriter()
        {
            var path = @"E:\~shares\dataRoot\Visual Studio 2010\Projects\Songhay.Tests\FrameworkFileTest.xml";
            var stream = new FileStream(path, FileMode.Create);
            try
            {
                using(var writer = new XmlTextWriter(stream, Encoding.UTF8))
                {
                    writer.WriteStartElement("root");
                    writer.WriteAttributeString("xmlns", "x", null, "urn:1");
                    writer.WriteStartElement("item", "urn:1");
                    writer.WriteString("This is the text to write to the test file.");
                    writer.WriteEndElement();
                    writer.WriteStartElement("item", "urn:1");
                    writer.WriteString("According to Notepad++, when Encoding.UTF8 is specified the encoding is UTF8.");
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }
            }
            finally
            {
                if(stream != null) stream.Dispose();
            }
        }

        [TestMethod]
        public void ShouldWriteToMyDocumentsFolder()
        {
            var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
                @"\WordWalkingStick\";
            if(!Directory.Exists(path)) Directory.CreateDirectory(path);
        }
    }
}
